package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import com.example.myapplication.adapters.ViewPagerAdapter;
import com.example.myapplication.fregments.BlankFragment;
import com.example.myapplication.fregments.BlankFragment2;
import com.example.myapplication.interfaces.IFragmentCallback;

import java.util.ArrayList;
import java.util.List;

public class FragmentActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);

        ViewPager viewPager = findViewById(R.id.view_pager1);

        //获取这个activity的渲染器
        LayoutInflater lf = LayoutInflater.from(this);
        //将layout中xml资源渲染成view对象
        View view1 = lf.inflate(R.layout.pager1, null);
        View view2 = lf.inflate(R.layout.pager2, null);
        View view3 = lf.inflate(R.layout.pager3, null);

        List<View> views = new ArrayList<>();
        views.add(view1);
        views.add(view2);
        views.add(view3);

        //将view加入适配器
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(views);

        //设置Pager的适配器
        viewPager.setAdapter(viewPagerAdapter);

        view2.findViewById(R.id.change_frag1).setOnClickListener(this);
        view2.findViewById(R.id.change_frag2).setOnClickListener(this);
        view2.findViewById(R.id.change_frag3).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_frag1:
                Bundle bundle = new Bundle();
                bundle.putString("message", "我喜欢睡觉");
                BlankFragment blankFragment = new BlankFragment();
                blankFragment.setArguments(bundle);
                replaceFragment(blankFragment);
                break;
            case R.id.change_frag2:
                replaceFragment(new ItemFragment());
                break;
            case R.id.change_frag3:
                BlankFragment2 blankFragment2 = new BlankFragment2();
                blankFragment2.setIFragmentCallback(new IFragmentCallback() {
                    @Override
                    public void sendMsgToActivity(String msg) {
                        //接到来自fragment的消息
                        Toast.makeText(FragmentActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public String getMsgFromActivity() {
                        return "来自activity的消息";
                    }
                });
                replaceFragment(blankFragment2);
                break;
        }
    }

    private void replaceFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

}